import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/common/app_colors.dart';
import 'package:finance_app/src/utils/language_utils.dart';
import 'package:finance_app/src/widgets/button/normal_button.dart';
import 'package:finance_app/src/widgets/select/normal_check_box.dart';
import 'package:flutter/material.dart';
import 'default_bottom_sheet.dart';

showReportFilterBottomSheet(
  BuildContext context, {
  List<String> selectFilter,
  List<String> defaultFilter,
}) =>
    showFilterBottomSheet(context,
        selectFilter: selectFilter,
        defaultFilter: defaultFilter,
        title: S.current.filter,
        by: S.current.by_category);

showFilterLeaveBottomSheet(
  BuildContext context, {
  List<String> selectFilter,
}) =>
    showFilterBottomSheet(context,
        selectFilter: selectFilter,
        defaultFilter: [
          'Pending',
          'Upcoming',
          'Rejected',
          'Past',
        ],
        title: S.current.filter,
        by: S.current.by_type);

showFilterServiceBottomSheet(
  BuildContext context, {
  List<String> selectFilter,
  List<String> defaultFilter,
}) =>
    showFilterBottomSheet(context,
        selectFilter: selectFilter,
        defaultFilter: defaultFilter,
        title: S.current.filter_service,
        by: S.current.by_category);

showFilterProductBottomSheet(
  BuildContext context, {
  List<String> selectFilter,
  List<String> defaultFilter,
}) =>
    showFilterBottomSheet(context,
        selectFilter: selectFilter,
        defaultFilter: defaultFilter,
        title: S.current.filter_product,
        by: S.current.by_category);

showFilterTransactionBottomSheet(
  BuildContext context, {
  List<String> selectFilter,
  List defaultFilter,
}) =>
    showFilterBottomSheet(context,
        selectFilter: selectFilter,
        defaultFilter: defaultFilter ??
            [
              'Quotation',
              'Purchase Order',
              'Invoice',
              'Bill',
              'Money-In',
              'Expense',
              'Money-Out',
            ],
        title: S.current.filter_transaction);

/// 交易订单筛选
showFilterBottomSheet(
  BuildContext context, {
  String title,
  String by,
  List<String> selectFilter,
  List<String> defaultFilter,
}) async {
  List<String> _currentSelectFilter = selectFilter;
  GlobalKey<FilterBottomSheetState> _key = GlobalKey();

  final result = await showDefaultBottomSheet(
    context,
    child: FilterBottomSheet(
      key: _key,
      value: selectFilter,
      selectCallBack: (data) {
        _currentSelectFilter = data;
      },
      group: defaultFilter,
    ),
    leading: DefaultBottomSheetLeadingButton(
      title: S.current.reset,
      onTap: () {
        _key.currentState.onClear();
      },
    ),
    title: title,
    isScrollControlled: true,
  );
  if (result != null) {
    return {
      'type': _currentSelectFilter,
      ...(result as Map),
    };
  }
  return _currentSelectFilter;
}

class FilterBottomSheet extends StatefulWidget {
  final List<String> value;
  final ValueChanged<List<String>> selectCallBack;
  final List<String> group;
  final String by;

  const FilterBottomSheet(
      {Key key, this.by, this.value, this.selectCallBack, this.group})
      : super(key: key);

  @override
  FilterBottomSheetState createState() => FilterBottomSheetState();
}

class FilterBottomSheetState extends State<FilterBottomSheet> {
  List<String> filterList = [];

  List<String> currentSelect = [];

  bool get enable => currentSelect.isNotEmpty;

  @override
  void initState() {
    super.initState();
    filterList = widget.group ?? [];

    currentSelect = widget.value;
  }

  void onChangeSalesTrend(String value) {
    if (currentSelect.contains(value)) {
      currentSelect.remove(value);
    } else {
      currentSelect.add(value);
    }
    setState(() {});
  }

  void _onApply() {
    widget.selectCallBack?.call(currentSelect);
    Navigator.of(context).pop();
  }

  void onClear() {
    setState(() {
      currentSelect.clear();
    });
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      mainAxisSize: MainAxisSize.min,
      children: [
        SizedBox(
          height: getDp(17),
        ),
        _buildTop(widget.by ?? S.current.by_type),
        SizedBox(
          height: getDp(17),
        ),
        Padding(
          padding: EdgeInsets.symmetric(
            horizontal: getDp(30),
          ),
          child: Wrap(
            crossAxisAlignment: WrapCrossAlignment.center,
            alignment: WrapAlignment.start,
            children: filterList
                .map((e) => PressedOpacityButton(
                      onPressed: () => onChangeSalesTrend(e),
                      padding: EdgeInsets.symmetric(
                        vertical: getDp(10),
                      ),
                      child: NormalCheckBox(
                        title: LanguageUtils.translateFilter(e),
                        minWidth: getDp(140),
                        isSelect: currentSelect.contains(e),
                      ),
                    ))
                .toList(),
          ),
        ),
        SizedBox(
          height: getDp(30),
        ),
        NormalButton(
          onTap: enable ? _onApply : null,
          title: S.current.apply,
        ),
      ],
    );
  }

  Widget _buildTop(String title) => Container(
        height: getDp(32),
        color: aceWhiteColor,
        padding: EdgeInsets.symmetric(horizontal: getDp(24)),
        alignment: Alignment.centerLeft,
        child: Text(
          title,
          style: TextStyle(
            color: aceDarkGrayColor,
            fontSize: getSp(16),
            fontWeight: FontWeight.w400,
            height: 22 / 16,
          ),
        ),
      );
}
